﻿layui.use(['table', 'layer', 'form'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var $ = layui.jquery;
    //表格初始化
    var tab = table.render({
        elem: '#demo',
        id: "tableId",
        method: 'get',
        url: 'http://localhost:8080/book/allBook',
        page: true,//开启分页
        limit: 5,
        limits: [5, 10, 15, 50],
        request: {
            pageName: "pageNum",
            limitName: "pageSize"
        },
        cols: [[ //表头
            {field: 'bookId', title: '书本编号'},
            {field: 'bookName', title: '书籍名称'},
            {field: 'bookCounts', title: '数量'},
            {field: 'detail', title: '书籍详情'},
            {
                title: '书籍类型', templet: function (d) {
                    if (d.type == 1) return "计算机技术";
                    else if (d.type == 2) return "投资之道";
                    else return "娱乐";
                }
            },
            /*使用templet进行单数据转换以及多层对象时内部属性获取*/
            {
                title: '储备情况', templet: function (d) {
                    return d.storage == 1 ? "储备充足" : "储备不足";
                }
            },
            {field: 'date', title: '入库时间'},
            {
                field: '', title: '操作 ', templet: function () {
                    var str = " <button type=\"button\" class=\"layui-btn layui-btn-xs  layui-btn-danger\" lay-event='delete'>删除</button>";
                    str += " <button type=\"button\" class=\"layui-btn layui-btn-xs layui-btn-warm\" lay-event='edit'>编辑</button>";
                    return str;
                }
            }

        ]],
        //否则无法解析数据
        parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.message,
                "count": res.count,
                data: res.list
            }
        }
    });
    //表格操作
    table.on('tool(book)', function (obj) {
        var data = obj.data;
        var event = obj.event;
        if (event == 'delete') {
            layer.confirm("是否删除", {icon: 3, title: "提示"}, function (index) {
                $.getJSON("http://localhost:8080/book/delBook", {id: data.bookId}, function (data) {
                    console.log(data);
                    if (data.code = 200) {
                        layer.msg("删除成功", {icon: 1}, 2000);
                        tab.reload();
                    } else {
                        layer.msg("删除失败，请仔细检查后再次操作", {icon: 1}, 2000);
                        return false;
                    }
                })
            })
        } else if (event == 'edit') {
            console.log('http://localhost:8080/view/toUpdateBook?bookId=' + data.bookId);
            layer.open({
                type: 2,
                title: '更新书籍',
                content: 'http://localhost:8080/view/toUpdateBook?bookId=' + data.bookId,
                area: ['600px', '500px'],
                //关闭页面后的执行动作
                end: function () {
                    tab.reload();
                },
            })
        }
    })
    //书籍插入功能实现
    $("#addBook").click(function () {
        layer.open({
            type: 2,
            title: '添加书籍',
            content: 'http://localhost:8080/view/toAddBook',
            area: ['500px', '500px'],
            end: function () {
                tab.reload();
            }
        })
    })
    //书籍批量导入功能实现
    $("#importBook").click(function () {
        layer.open({
            type: 2,
            title: '添加书籍',
            shadeClose: true,
            content: 'http://localhost:8080/view/toBatchBook',
            area: ['500px', '500px'],
            cancel: function (index) {
                layer.confirm('确定要关闭吗?', {icon: 3, title: '提示'}, function (i) {
                    layer.close(index);
                    layer.close(i);
                });
                return false;
            }
        })
    })
    //书籍批量导出功能实现
    $("#exportBook").click(function () {
        location.href = "http://localhost:8080/book/exportBook";
        /*
          $.getJSON("http://localhost:8080/book/exportBook", function (data) {
          console.log(data);
          layer.msg("批量导出成功", {icon: 1}, 2000);
        })*/
    })
    //监听表单按钮
    form.on('submit(query)', function (data) {
        tab.reload({
            where: data.field,
            page: {
                curr: 1
            }
        });
        return false;
    });
});
